package com.itestor.iptv.modules.common.service.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.hibernate.Query;
import com.itestor.iptv.modules.common.bean.GroupManageQuery;
import com.itestor.iptv.modules.common.dao.TbGroupManageDao;
import com.itestor.iptv.modules.common.domain.GroupManage;
import com.itestor.iptv.modules.common.service.GroupManageService;
import com.itestor.iptv.modules.common.vo.GroupManageOBJ;
import com.itestor.iptv.utils.BeanConverter;

/**
 * 
 * @author Mr.li
 * 
 */
public class GroupManageServiceImpl implements GroupManageService {

    private static final Logger logger = Logger
            .getLogger(GroupManageServiceImpl.class);

    private TbGroupManageDao tbGroupManageDao;

    public GroupManageOBJ get(String groupName) {
        GroupManageOBJ user = null;
        GroupManage tbGroupManage = tbGroupManageDao.find(groupName);
        if (tbGroupManage != null) {
            user = convertToGroupManageObj(tbGroupManage);
        }
        return user;
    }

    
    public GroupManageOBJ get(int id) {
        GroupManageOBJ groupManageObj = null;
        GroupManage tbGroupManage = tbGroupManageDao.find(id);
        if (tbGroupManage != null) {
            groupManageObj = convertToGroupManageObj(tbGroupManage);
        }
        return groupManageObj;
    }
    
    /**
     * 
     * @param groupManageQuery
     */
    @SuppressWarnings("unchecked")
    public void queryGroupManageList(GroupManageQuery groupManageQuery) {        
        Collection<GroupManageOBJ> grouplist = new ArrayList<GroupManageOBJ>();
        
        StringBuilder hql = new StringBuilder();
        hql.append("from GroupManage t ");
        if (!StringUtils.isEmpty(groupManageQuery.getGroupName())) {
            hql.append(" where t.groupName like '%" + groupManageQuery.getGroupName()
                    + "%'");
        }
        Query query = tbGroupManageDao.getHibernateSession().createQuery(
                hql.toString());
        
        if (groupManageQuery.getPagerCount() == -1) {
            groupManageQuery.setPagerCount(query.list().size());
        }
        
        query.setFirstResult(groupManageQuery.getPagerOffset());
        query.setMaxResults(groupManageQuery.getPagerPerPage());
        
        for (Iterator it = query.iterate(); it.hasNext();) {
            grouplist.add(convertToGroupManageObj((GroupManage) it.next()));
        }
        
        groupManageQuery.setQueryResults(grouplist);
    }
    
    /**
     * 新增虚拟分组
     * @param groupManage
     */
    public void add(GroupManageOBJ groupManage) {
        this.tbGroupManageDao.create(convertToGroupManage(groupManage));
    }
    
    /**
     * 删除虚拟分组
     * @param ids
     */
    public void delete(int[] ids) {
        for (int id : ids) {
            tbGroupManageDao.delete(id);
        }
    }

    /**
     * 修改虚拟分组
     * @param group     
     */
    public void modify(GroupManageOBJ group) {              
        if (tbGroupManageDao.find(group.getIdGroup()) != null) {
            tbGroupManageDao.update(convertToGroupManage(group));
        }
    }
    
    /**
     * 获取所有分组列表.
     * @return List
     */
    public List<GroupManage> findAllGroupManage(){
        return (List<GroupManage>) tbGroupManageDao.findAll();
    }
    
    /**
     * 分配设备
     * @param idGroup
     * @param robotIdList
     */
    public void distributeRobot(int idGroup, List<Integer> robotIdList){
        for (Integer robotId :robotIdList){
            tbGroupManageDao.mergeGroupManageRobotRelation(idGroup,robotId);    
        }        
    }
    
    /**
     *  
     * @param pGroupManageId 
     * @param pIds  
     */
    public void deleteRelRobot(String pGroupManageId, int[] pIds){
        for (int id : pIds) {
            tbGroupManageDao.deleteRelRobot(Integer.parseInt(pGroupManageId),id);
        }        
    }
    
    
    /**
     * @param tbGroupManage GroupManage(po)
     * @return group GroupManageObj(vo)
     */
    public GroupManageOBJ convertToGroupManageObj(GroupManage tbGroupManage) {
        GroupManageOBJ group = new GroupManageOBJ();
        BeanConverter.copyProperties(group, tbGroupManage);
        return group;
    }
    
    /** 
     * @param group
     * @return GroupManage
     */
    public GroupManage convertToGroupManage(GroupManageOBJ group) {
        GroupManage tbGroupManage = new GroupManage();
        BeanConverter.copyProperties(tbGroupManage, group);        
        return tbGroupManage;
    }
    
    /**
     *  
     * @param pTbGroupManageDao
     */
    public void setTbGroupManageDao(TbGroupManageDao pTbGroupManageDao) {
        tbGroupManageDao = pTbGroupManageDao;
    }
    

}
